<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>美食专家 - 菜品制作查询</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .container {
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            padding: 40px;
            max-width: 800px;
            width: 100%;
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
        }

        .header h1 {
            color: #333;
            font-size: 2.5em;
            margin-bottom: 10px;
            background: linear-gradient(45deg, #667eea, #764ba2);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }

        .header p {
            color: #666;
            font-size: 1.1em;
        }

        .input-section {
            margin-bottom: 30px;
        }

        .input-group {
            display: flex;
            gap: 15px;
            margin-bottom: 20px;
        }

        .input-field {
            flex: 1;
            padding: 15px 20px;
            border: 2px solid #e1e5e9;
            border-radius: 10px;
            font-size: 16px;
            transition: all 0.3s ease;
        }

        .input-field:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .search-btn {
            padding: 15px 30px;
            background: linear-gradient(45deg, #667eea, #764ba2);
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.3s ease;
            min-width: 120px;
        }

        .search-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }

        .search-btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }

        .loading {
            display: none;
            text-align: center;
            margin: 20px 0;
        }

        .loading-spinner {
            border: 3px solid #f3f3f3;
            border-top: 3px solid #667eea;
            border-radius: 50%;
            width: 30px;
            height: 30px;
            animation: spin 1s linear infinite;
            margin: 0 auto 10px;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .result-section {
            display: none;
        }

        .result-card {
            background: #f8f9fa;
            border-radius: 15px;
            padding: 25px;
            margin-bottom: 20px;
            border-left: 5px solid #667eea;
        }

        .result-title {
            font-size: 1.5em;
            color: #333;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .result-title::before {
            content: '🍽️';
            font-size: 1.2em;
        }

        .info-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 25px;
        }

        .info-section {
            background: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
        }

        .info-section h3 {
            color: #667eea;
            margin-bottom: 15px;
            font-size: 1.2em;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .ingredients h3::before {
            content: '🥬';
        }

        .methods h3::before {
            content: '👨‍🍳';
        }

        .info-list {
            list-style: none;
        }

        .info-list li {
            padding: 8px 0;
            border-bottom: 1px solid #eee;
            position: relative;
            padding-left: 20px;
        }

        .info-list li:last-child {
            border-bottom: none;
        }

        .info-list li::before {
            content: '•';
            color: #667eea;
            font-weight: bold;
            position: absolute;
            left: 0;
        }

        .error-message {
            background: #fee;
            color: #c33;
            padding: 15px;
            border-radius: 10px;
            margin: 20px 0;
            border-left: 5px solid #c33;
            display: none;
        }

        .example-dishes {
            margin-top: 20px;
            text-align: center;
        }

        .example-dishes p {
            color: #666;
            margin-bottom: 10px;
        }

        .dish-tags {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            justify-content: center;
        }

        .dish-tag {
            background: #e8f0fe;
            color: #667eea;
            padding: 5px 15px;
            border-radius: 20px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-size: 14px;
        }

        .dish-tag:hover {
            background: #667eea;
            color: white;
            transform: translateY(-2px);
        }

        @media (max-width: 768px) {
            .container {
                padding: 20px;
                margin: 10px;
            }

            .input-group {
                flex-direction: column;
            }

            .info-grid {
                grid-template-columns: 1fr;
            }

            .header h1 {
                font-size: 2em;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🍳 美食专家</h1>
            <p>输入菜品名称，获取详细的制作配料和制作方法</p>
        </div>

        <div class="input-section">
            <div class="input-group">
                <input type="text" id="dishInput" class="input-field" placeholder="请输入菜品名称，如：宫保鸡丁、红烧肉、麻婆豆腐..." maxlength="50">
                <button id="searchBtn" class="search-btn">查询制作方法</button>
            </div>
            
            <div class="example-dishes">
                <p>热门菜品推荐：</p>
                <div class="dish-tags">
                    <span class="dish-tag" onclick="searchDish('宫保鸡丁')">宫保鸡丁</span>
                    <span class="dish-tag" onclick="searchDish('红烧肉')">红烧肉</span>
                    <span class="dish-tag" onclick="searchDish('麻婆豆腐')">麻婆豆腐</span>
                    <span class="dish-tag" onclick="searchDish('糖醋里脊')">糖醋里脊</span>
                    <span class="dish-tag" onclick="searchDish('鱼香肉丝')">鱼香肉丝</span>
                    <span class="dish-tag" onclick="searchDish('回锅肉')">回锅肉</span>
                </div>
            </div>
        </div>

        <div class="loading" id="loading">
            <div class="loading-spinner"></div>
            <p>AI美食专家正在为您分析菜品制作方法...</p>
        </div>

        <div class="error-message" id="errorMessage"></div>

        <div class="result-section" id="resultSection">
            <div class="result-card">
                <div class="result-title" id="dishName"></div>
                <div class="info-grid">
                    <div class="info-section ingredients">
                        <h3>制作配料</h3>
                        <ul class="info-list" id="ingredientsList"></ul>
                    </div>
                    <div class="info-section methods">
                        <h3>制作方法</h3>
                        <ul class="info-list" id="methodsList"></ul>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        const dishInput = document.getElementById('dishInput');
        const searchBtn = document.getElementById('searchBtn');
        const loading = document.getElementById('loading');
        const errorMessage = document.getElementById('errorMessage');
        const resultSection = document.getElementById('resultSection');
        const dishName = document.getElementById('dishName');
        const ingredientsList = document.getElementById('ingredientsList');
        const methodsList = document.getElementById('methodsList');

        // 绑定回车键事件
        dishInput.addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                searchRecipe();
            }
        });

        // 绑定搜索按钮事件
        searchBtn.addEventListener('click', searchRecipe);

        // 快速搜索函数
        function searchDish(dish) {
            dishInput.value = dish;
            searchRecipe();
        }

        // 主要搜索函数
        async function searchRecipe() {
            const dish = dishInput.value.trim();
            
            if (!dish) {
                showError('请输入菜品名称');
                return;
            }

            // 显示加载状态
            showLoading();
            hideError();
            hideResult();

            try {
                const response = await fetch(`/api/v2/chat/recipe?dishName=${encodeURIComponent(dish)}`);
                
                if (!response.ok) {
                    throw new Error(`HTTP ${response.status}: ${response.statusText}`);
                }

                const data = await response.json();
                
                // 显示结果
                displayResult(data);
                
            } catch (error) {
                console.error('查询失败:', error);
                showError('查询失败，请检查网络连接或稍后重试。错误信息: ' + error.message);
            } finally {
                hideLoading();
            }
        }

        // 显示结果
        function displayResult(data) {
            dishName.textContent = data.dishName;
            
            // 显示配料
            ingredientsList.innerHTML = '';
            if (data.ingredients && data.ingredients.length > 0) {
                data.ingredients.forEach(ingredient => {
                    const li = document.createElement('li');
                    li.textContent = ingredient;
                    ingredientsList.appendChild(li);
                });
            } else {
                const li = document.createElement('li');
                li.textContent = '暂无配料信息';
                li.style.color = '#999';
                ingredientsList.appendChild(li);
            }

            // 显示制作方法
            methodsList.innerHTML = '';
            if (data.methods && data.methods.length > 0) {
                data.methods.forEach((method, index) => {
                    const li = document.createElement('li');
                    li.innerHTML = `<strong>步骤${index + 1}:</strong> ${method}`;
                    methodsList.appendChild(li);
                });
            } else {
                const li = document.createElement('li');
                li.textContent = '暂无制作方法信息';
                li.style.color = '#999';
                methodsList.appendChild(li);
            }

            showResult();
        }

        // 显示/隐藏函数
        function showLoading() {
            loading.style.display = 'block';
            searchBtn.disabled = true;
            searchBtn.textContent = '查询中...';
        }

        function hideLoading() {
            loading.style.display = 'none';
            searchBtn.disabled = false;
            searchBtn.textContent = '查询制作方法';
        }

        function showError(message) {
            errorMessage.textContent = message;
            errorMessage.style.display = 'block';
        }

        function hideError() {
            errorMessage.style.display = 'none';
        }

        function showResult() {
            resultSection.style.display = 'block';
        }

        function hideResult() {
            resultSection.style.display = 'none';
        }

        // 页面加载完成后自动聚焦输入框
        window.addEventListener('load', function() {
            dishInput.focus();
        });
    </script>
</body>
</html>